package com.zhuojy.equity.mall.admin.manager;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.common.pojo.TreeVO;
import com.zhuojy.equity.mall.admin.common.utils.TreeUtil;
import com.zhuojy.equity.mall.admin.dal.dataobject.TMenuDO;
import com.zhuojy.equity.mall.admin.dal.mapper.TMenuMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单表数据管理层
 */
@Component
@RequiredArgsConstructor
public class MenuManager{
    private final TMenuMapper tMenuMapper;
    @Autowired
    private TreeUtil treeUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取菜单信息
     * @param id 菜单id
     * @return
     */
    public ResultEntity queryMenuById(String id) {
        List<TreeVO> o =(List<TreeVO>) redisTemplate.opsForHash().get("menu", id);
        if (CollectionUtils.isEmpty(o)){
            List<TMenuDO> sysMenus = tMenuMapper.selectList(null);
            List<TreeVO> collect = sysMenus.stream().map(sysMenuss -> {
                TreeVO treeVO = new TreeVO();
                BeanUtils.copyProperties(sysMenuss, treeVO);
                return treeVO;
            }).collect(Collectors.toList());
            o = treeUtil.tree(collect,id);
            redisTemplate.opsForHash().put("menu",id,o);
        }
        return ResultEntity.OK(o);

    }
}
